﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
   public static  class 销售退货单BLL
    {
        public static void 保存单据(DanjuTable danju,List <danjumingxitable > danjumingxitables )
        {
            
                danju.dh = 单据BLL.检查单号(danju.dh);
                danju.je = 0 - danju.je;
                DanjuTableService.InsertDanjuTable(danju);            
                foreach (var m in danjumingxitables.Where(x => x.Bianhao != null).ToList())
                {
                    m.danhao = danju.dh;
                    m.chengpingmishu = 0 - m.chengpingmishu;
                    m.chengpingjuanshu = 0 - m.chengpingjuanshu;
                    m.hanshuidanjia = 0 - m.hanshuidanjia;
                    m.hanshuiheji = 0 - m.hanshuiheji;
                    danjumingxitableService.Insertdanjumingxitable(m);
                }
                if (SysInfo.GetInfo.own != string.Empty)
                {
                    if (SysInfo.GetInfo.own != "审核制")
                    {
                        单据审核(danju.dh);
                    }
                }
                else
                {
                    单据审核(danju.dh);
                }        
        }
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            System.Threading.Thread.Sleep(500);
            danju.je = 0 - danju.je;
            foreach (var m in danjumingxitables.Where(x => x.Bianhao != null).ToList())
            {
                m.danhao = danju.dh;
                m.chengpingmishu = 0 - m.chengpingmishu;
                m.chengpingjuanshu = 0 - m.chengpingjuanshu;
                m.hanshuidanjia = 0 - m.hanshuidanjia;
                m.hanshuiheji = 0 - m.hanshuiheji;
                danjumingxitableService.Insertdanjumingxitable(m);
            }
            DanjuTableService.UpdateDanjuTable (danju,x=>x.dh==danju.dh );
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danju.dh);
            foreach (var m in danjumingxitables.Where(x => x.Bianhao != null).ToList())
            {
                danjumingxitableService.Insertdanjumingxitable(m);
            }
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
        }
        public static void 删除单据(string danhao)
        {
            销售发货单BLL.删除单据(danhao);
            //if (SysInfo.GetInfo.own != string.Empty)
            //{
            //    if (SysInfo.GetInfo.own != "审核制")
            //    {
            //        单据反审核(danhao);
            //    }
            //}
            //DanjuTableService.DeleteDanjuTable(x => x.dh == danhao);
            //danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danhao);
        }
        public static void 单据审核(string danhao)
        {
            销售发货单BLL.单据审核(danhao);
            //var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            //var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);
            //danju.zhuangtai = "已审核";
            //DanjuTableService.UpdateDanjuTable(danju, x => x.dh == danhao);
            //来往明细BLL.增加来往记录(danju);
            //财务BLL.增加应收款  (danju);
            //财务BLL.增加应开发票 (danju);
            //订单BLL.减少费用(danjumingxitables, danju);
            //库存BLL.增加库存 (danjumingxitables, danju);
            //单据BLL.审核(danhao);
            //订单进度BLL.新增进度(danjumingxitables, danju);
            //订单BLL.增加发货金额(danjumingxitables);
            //订单BLL.增加已发货数量(danjumingxitables);
        }
        public static void 单据反审核(string danhao)
        {
            销售发货单BLL.单据反审核(danhao);
            //var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            //var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);
            //danju.zhuangtai = "未审核";
            //DanjuTableService.UpdateDanjuTable(danju, x => x.dh == danhao);
            //来往明细BLL.删除来往记录(danju);
            //财务BLL.减少应收款 (danju);
            //财务BLL.减少应开发票  (danju);
            //订单BLL.增加费用(danjumingxitables, danju);
            //库存BLL.减少库存  (danjumingxitables, danju);
            //单据BLL.未审核(danhao);
            //订单进度BLL.删除进度(danju.dh);
            //订单BLL.减少发货金额 (danjumingxitables);
            //订单BLL.减少已发货数量(danjumingxitables);
        } 
    }
}
